/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.storage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.enfry.storage.BaseService;
import com.jweb.framework.core.Context;
import com.enfry.storage.exception.ExCode;
import com.enfry.storage.exception.StorageException;
import com.enfry.storage.api.IFileOperate;

/**
 * 删除service
 * @Title com.enfry.storage.service.FileDeleteService
 * @author liulei
 * @date 2016年7月25日
 */
public class FileDeleteService extends BaseService {

    @Resource
    private IFileOperate fileOperate;
    
    public void delete(Context ctx) {
        try {
            String fileCode = ctx.getData("fileCode");
            fileOperate.delete(fileCode);
            this.ajaxSuccess(ctx, "");
        } catch (StorageException e) {
            logger.error(e.getDefaultMessage());
            this.ajaxError(ctx, e);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            this.ajaxError(ctx, ExCode.RUNTIME);
        }
    }
    
    public void deleteBatch(Context ctx) {
        try {
            String[] fileCodes = ((String)ctx.getData("fileCodes")).split(",");
            List<String> errorIds = fileOperate.deleteBatch(fileCodes);
            if(errorIds.isEmpty())
                this.ajaxSuccess(ctx);
            else {
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("errorIds", errorIds);
                this.ajaxError(ctx, ExCode.FILE_DELETE_BATCH_FAIL, data);
            }
        } catch (StorageException e) {
            logger.error(e.getDefaultMessage());
            this.ajaxError(ctx, e);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            this.ajaxError(ctx, ExCode.RUNTIME);
        }
    }
}
